<template>
  <div v-loading="loading">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:dd:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle(null)"
        >{{ $t("新增") }}</el-button>
      </el-col>
    </el-row>
    <el-table :data="dealDdItemList" tooltip-effect="darkTable" :span-method="objectSpanMethod" class="noneHoverBg" border>
      <el-table-column :label="$t('尽调清单大类')" align="center" prop="categoriesType" :formatter="categoriesTypeFormat" />
      <el-table-column :label="$t('尽调清单子类')" align="center" prop="categoriesChildTypeFormat" />
      <el-table-column :label="$t('排序')" align="center" prop="sort" />
      <el-table-column :label="$t('修改时间')" align="center" prop="updateTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['deal:dd:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['deal:dd:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="$t('尽调项')" :visible.sync="formOpen" width="60%" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealDdListForm ref="dealDdListForm" />
    </el-dialog>
  </div>
</template>

<script>
  import { ddItemList, delDealDdList } from '@/api/deal/dd/dealDdList'
  import dealDdListForm from '@/views/deal/dd/dealDdListForm'
    export default {
      components: {
        dealDdListForm
      },
      data() {
        return {
          // 遮罩层
          loading: false,
          // 尽调项表格数据
          dealDdItemList: [],
          // 弹出层标题
          title: '',
          // 是否显示弹出层
          formOpen: false,
          form: {},
          queryParams: {
            itemName: undefined
          },
          // 尽调清单大类
          categoriesTypeOptions: [],
          categoriesTypeChildrenOptions: []
        }
      },
      created() {
        this.getDicts('dd_categories_type').then(response => {
          this.categoriesTypeOptions = response.data
        })
      },
      methods: {
        reset() {
          this.form = {
            id: undefined,
            ddItemOptions: [],
            ddId: this.$route.params.ddId,
            parentId: undefined,
            itemName: undefined,
            sort: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined
          }
          this.resetForm('form')
        },
        ddItemList() {
          this.loading = true
          ddItemList({ 'ddId': this.$route.params.ddId }).then(response => {
            this.dealDdItemList = response.data
            this.loading = false
          })
        },
        addHandle(row) {
          this.formOpen = true
          this.$nextTick(() => {
            if (row != null) {
              this.$refs.dealDdListForm.init({ 'itemId': row.id })
            } else {
              this.$refs.dealDdListForm.init(null)
            }
          })
        },
        updateHandle(row) {
          this.formOpen = true
          this.$nextTick(() => {
            this.$refs.dealDdListForm.init(row)
          })
        },
        /** 删除按钮操作 */
        deleteHandle(row) {
          this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
          }).then(function() {
            return delDealDdList({ 'id': row.id, 'ddId': row.ddId })
          }).then(() => {
            this.ddItemList({ 'id': row.ddId })
            this.msgSuccess(this.$t('删除成功'))
          }).catch(function() {
          })
        },
        cancel() {
          this.$parent.$parent.ddListIndexOpen = false
          this.$parent.$parent.getList()
        },
        // 尽调清单大类字典翻译
        categoriesTypeFormat(row) {
          return this.selectDictLabel(this.categoriesTypeOptions, row.categoriesType)
        },
        rowspan(data) {
          // 二维数组，用于存放单元格合并规则
          const spanArr = []
          // 用于存储相同项的开始index
          let position = 0
          data.forEach((item, index) => {
            if (index === 0) {
              spanArr.push(1)
              position = 0
            } else {
              if (data[index].categoriesType === data[index - 1].categoriesType) {
                spanArr[position] += 1 // 有相同项
                spanArr.push(0) // 名称相同后往数组里面加一项0
              } else {
                spanArr.push(1) // 同列的前后两行单元格不相同
                position = index
              }
            }
          })
          return spanArr
        },
        objectSpanMethod({ rowIndex, columnIndex }) {
          if (columnIndex === 0) {
            const spanArr = this.rowspan(this.dealDdItemList)
            for (let i = 0; i < spanArr.length; i++) {
              const _row = spanArr[rowIndex]
              return {
                rowspan: _row,
                colspan: 1
              }
            }
          }
        }
      }
    }
</script>

<style scoped>

</style>
